/******************************************************************************/
package jamie.org.sil.www.UsxVernacularCreator;

import java.awt.Component;
import javax.swing.*;
import javax.swing.table.*;


/******************************************************************************/
/**
 * Table used to handle user input.
 * <p>
 *
 * Much of this is driven by the needs of JTable, which I don't profess to
 * understand properly.
 */

public class GuiInputTable extends JTable
{
  /****************************************************************************/
  /****************************************************************************/
  /**                                                                        **/
  /**                               Public                                   **/
  /**                                                                        **/
  /****************************************************************************/
  /****************************************************************************/

  /****************************************************************************/
  /** Constructor.

      @param tabNo Pane number.
   */

  public GuiInputTable (int tabNo)
  {
    super(new GuiInputTableModel(tabNo));
    m_TabNo = tabNo;
    fitPrompts();
    setTableHeader(null);
  }


  /****************************************************************************/
  @Override public void changeSelection (int row, int column, boolean toggle, boolean extend)
  {
    int n = XmlDetails.instance().getTagCount(m_TabNo);
    while (true)
    {
      if (XmlDetails.instance().getTag(m_TabNo, row).isEditable(1)) break;
      if (row == n) break;
      ++row;
    }

    super.changeSelection(row, 1, toggle, extend);
    editCellAt(row, 1);
    transferFocus();
  }


  /****************************************************************************/
  /** Gets the cell renderer.
   
      @param row    Row of table cell.
      @param column Column of table cell.
      @return Cell renderer.
   */
  
  @Override public TableCellRenderer getCellRenderer (int row, int column)
  {
    return XmlDetails.instance().getTag(m_TabNo, row).getRenderer(column);
  }


  /****************************************************************************/
  /** Gets the cell editor.

      @param row    Row of table cell.
      @param column Column of table cell.
      @return Cell editor.
   */

  @Override public TableCellEditor getCellEditor (int row, int column)
  {
    return XmlDetails.instance().getTag(m_TabNo, row).getEditor(column);
  }


  /****************************************************************************/
  /** Gets the preferred row height -- the maximum of all row heights, with
      an extra margin.
   *
      @param row
      @param margin
      @return Row height
   */
  public int getPreferredRowHeight (int row, int margin)
  {
    int height = getRowHeight();

    for (int col = 0; col < getColumnCount(); ++col)
    {
      TableCellRenderer renderer = getCellRenderer(row, col);
      Component comp = prepareRenderer(renderer, row, col);
      int h = comp.getPreferredSize().height + 2 * margin;
      height = Math.max(height, h);
    }

    return height;
  }


  /****************************************************************************/
  /** Makes all rows to be the height needed by the tallest row.

     @param margin
   */

  public void packRows (int margin)
  {
    int height = 0;
    int nRows = getRowCount();
    for (int row = 0; row < nRows; ++row) height = Math.max(height, getPreferredRowHeight(row, margin));
    for (int row = 0; row < nRows; ++row) if (getRowHeight(row) != height) setRowHeight(row, height);  }







  /****************************************************************************/
  /****************************************************************************/
  /**                                                                        **/
  /**                              Private                                   **/
  /**                                                                        **/
  /****************************************************************************/
  /****************************************************************************/

  /****************************************************************************/
  private int m_TabNo = -1;


  /****************************************************************************/
  /* Fit the left-hand column to the prompts, and make the right-hand column
     fill the remaining space. */
  
  private void fitPrompts ()
  {
    int width = 0;

    setAutoResizeMode(JTable.AUTO_RESIZE_LAST_COLUMN);

    for (int row =0; row < getRowCount(); ++row)
    {
      TableCellRenderer renderer = getCellRenderer(row, 0);
      Component comp = renderer.getTableCellRendererComponent(this, getValueAt(row, 0), true, true, row, 0);
      width = Math.max(width, comp.getPreferredSize().width);
    }

    getColumnModel().getColumn(0).setMinWidth(2 + width);
    getColumnModel().getColumn(0).setMaxWidth(2 + width);
  }
}
